package com.treetao.union.db.model.vo;

import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;

/**
 * ResponseBody构造器。一般用于ajax、rest等类型的Web服务
 */
@SuppressWarnings("serial")
public class RestResponse extends HashMap<String, Object> {
    public static RestResponse success(){
        return success("成功");
    }
    public static RestResponse success(Object obj){
        RestResponse restResponse = new RestResponse();
        restResponse.setSuccess(true);
        restResponse.setMessage("成功");
        restResponse.setData(obj);
        return restResponse;
    }
    public static RestResponse success(String message){
        RestResponse restResponse = new RestResponse();
        restResponse.setSuccess(true);
        restResponse.setMessage(message);
        return restResponse;
    }

    public static RestResponse failure(String message){
        RestResponse restResponse = new RestResponse();
        restResponse.setSuccess(false);
        restResponse.setMessage(message);
        return restResponse;
    }


    public RestResponse setSuccess(Boolean success) {
        if (success != null) put("success", success);
        return this;
    }

    public RestResponse setMessage(String message) {
        if (message != null) put("message", message);
        return this;
    }

    public RestResponse setData(Object data) {
        if (data != null) put("data", data);
        return this;
    }

    public RestResponse setPage(Long l) {
        if (l != null) put("page", l);
        return this;
    }
    
    public RestResponse setCurrentPage(Long l){
    	if (l != null) put("currentPage", l);
        return this;
    }

    public RestResponse setLimit(Integer limit) {
        if (limit != null) put("limit", limit);
        return this;
    }

    public RestResponse setTotal(Long total) {
        if (total != null) put("total", total);
        return this;
    }

    public RestResponse setAny(String key, Object value) {
        if (key != null && value != null) put(key, value);
        return this;
    }
	public RestResponse setCode(int code) {
		 put("code", code);
	        return this;
	}
	
	 
    /**
     * 只返回true/false标识
     * @param data
     * @return
     */
    public RestResponse setDataOnlyResult(Boolean data) {
        if (data != null) put("data", data);
        return this;
    }
    
    /**
     * 分页接口返回数据
     * @param data
     * @return
     */
    public RestResponse setPageData(Page  data) {
        if (data != null) {
        	setCurrentPage(data.getCurrent());
        	setPage(data.getPages());
        	setTotal(data.getTotal());
        	setData(data.getRecords());
        }
        return this;
    }

    private RestResponse setTotal(Integer i) {
        if (i != null) put("total", i);
        return this;
    }

    /**
     * 设置任何键值对数据
     * @param key
     * @param value
     * @return
     */
    public RestResponse setData(String key, Object value) {
        if (key != null && value != null) {
        	Map<String, Object> map = Maps.newHashMap();
        	map.put(key, value);
        	put("data",map);
        }
        return this;
    }
}
